package cz.muni.fi.pa165.musiclibrary.musiclibraryweb.components;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.cycle.RequestCycle;
import org.apache.wicket.spring.injection.annot.SpringBean;

import cz.muni.fi.pa165.musiclibrary.api.service.IGenreService;
import cz.muni.fi.pa165.musiclibrary.api.service.auth.IUser;
import cz.muni.fi.pa165.musiclibrary.api.service.dto.IGenreDTO;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.MusicLibrarySession;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.utils.DTOFactory;

/**
 * Panel that contains form for adding or editing a genre.
 * After successful operation an event with payload {@link FormSubmittedEvent}
 * is sent.
 * 
 * @author Maros Kucbel
 * @date 2012-11-25T23:16:13+0100
 */
public final class GenreFormPanel extends Panel {

    private IGenreDTO genre;
    @SpringBean
    private IGenreService genreService;
    
    public GenreFormPanel(String id) {
        this(id, DTOFactory.create(IGenreDTO.class));
    }
    
    public GenreFormPanel(String id, IGenreDTO genre) {
        super(id);
        this.genre = genre;
    }
    
    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.initComponents();
    }
    
    private void initComponents() {
        final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        feedbackPanel.setOutputMarkupId(true);
        this.add(feedbackPanel);
        
        Form<IGenreDTO> form = new Form<IGenreDTO>(ComponentIDs.FORM, Model.of(this.genre)) {

            @Override
            protected void onSubmit() {
                IGenreDTO genre = this.getModelObject();
                try {
                    IUser user = MusicLibrarySession.get().getUser();
                    String userName = user != null ? user.getUserName() : null;
                    if (genre.getIdGenre() != null) {
                        GenreFormPanel.this.genreService.update(genre, userName);
                    } else {
                        GenreFormPanel.this.genreService.create(genre, userName);
                    }
                    this.send(this.getPage(), Broadcast.BREADTH, new FormSubmittedEvent(RequestCycle.get().find(AjaxRequestTarget.class)));
                } catch (Exception ex) {
                    this.error(new StringResourceModel("database.error", this, null).getObject());
                    RequestCycle.get().find(AjaxRequestTarget.class).add(feedbackPanel);
                }
            }

        };
        RequiredTextField<String> nameField = new RequiredTextField<String>(ComponentIDs.NAME, new PropertyModel<String>(form.getModelObject(), "name"));
        nameField.setOutputMarkupId(true);
        form.add(nameField);
        form.add(new AjaxSubmitLinkWithErrorClass(ComponentIDs.SUBMIT_BUTTON, form) {

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.add(feedbackPanel);
                super.onError(target, form);
            }
        });
        this.add(form);
    }
    
    private static final class ComponentIDs {
        private static final String FORM = "form";
        private static final String NAME = "name";
        private static final String SUBMIT_BUTTON = "submit";
    }
}